package com.example.lj.uav.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lj.uav.R;
import com.example.lj.uav.bean.RouteLine;
import com.example.lj.uav.holder.RouteHeaderHolder;
import com.example.lj.uav.holder.RouteHolder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RouteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<RouteLine> dataList;
    private int TYPE_HEADER = 1001;
    Handler handler;
    public static final int ROUTE_SELECTED = 10;
    public static final int CREATE_SELECTED = 11;
    public static final int IMPORT_SELECTED = 12;
    public static final int ROUTE_CHECKED = 14;
    public static final int ROUTE_CHECKED_CANCEL = 15;
    public boolean headerShow = true;
    public boolean isSearching = false;


    public RouteAdapter(List dataList,Handler handler) {
        this.dataList = dataList;
        this.handler = handler;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==TYPE_HEADER){
            View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_header, parent, false);
            return new RouteHeaderHolder(headerView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route,parent,false);
        return new RouteHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position==0&&headerShow){
            RouteHeaderHolder routeHeaderHolder = (RouteHeaderHolder) holder;
            routeHeaderHolder.ll_create.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Message msg = handler.obtainMessage();
                    msg.what = CREATE_SELECTED;
                    handler.sendMessage(msg);
                }
            });

            routeHeaderHolder.ll_import.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Message msg = handler.obtainMessage();
                    msg.what = IMPORT_SELECTED;
                    handler.sendMessage(msg);
                }
            });
        }else{
            final RouteHolder routeHolder = (RouteHolder) holder;
            routeHolder.tv_place.setText(dataList.get(position).getUpdatePlace());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Bitmap bitmap = getBitmap(dataList.get(position).getImagePath(),340,340);
            if (bitmap!=null){
                routeHolder.iv_screenshoot.setImageBitmap(bitmap);
            }
            String time = format.format(new Date(dataList.get(position).getUpdateTime()));
            routeHolder.tv_time.setText(time);
            routeHolder.tv_name.setText(dataList.get(position).getFileName());
            routeHolder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Message msg = handler.obtainMessage();
                    msg.what = ROUTE_SELECTED;
                    msg.arg1 = position;
                    handler.sendMessage(msg);
                }
            });
            if (dataList.get(position).getCollected()==1){
                routeHolder.iv_collected_route.setVisibility(View.VISIBLE);
            }else{
                routeHolder.iv_collected_route.setVisibility(View.GONE);
            }
            if (headerShow){
                routeHolder.rl_select.setVisibility(View.GONE);
            }else{
                if (isSearching){//和查询排序共用adapter，传入flag避免出现选择功能
                    routeHolder.rl_select.setVisibility(View.GONE);
                }else{
                    routeHolder.rl_select.setVisibility(View.VISIBLE);
                }
                boolean checked = dataList.get(position).isChecked();
                if (checked){
                    routeHolder.cb_select.setChecked(true);
                }else{
                    routeHolder.cb_select.setChecked(false);
                }
                routeHolder.rl_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (routeHolder.cb_select.isChecked()){
                            dataList.get(position).setChecked(false);
                            handler.sendEmptyMessage(ROUTE_CHECKED_CANCEL);
                        }else{
                            dataList.get(position).setChecked(true);
                            handler.sendEmptyMessage(ROUTE_CHECKED);
                        }
                        notifyDataSetChanged();
                    }
                });

                routeHolder.cb_select.setClickable(false);
            }
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在第一个位置添加头
        if (position==0&&headerShow){
            return TYPE_HEADER;
        }
        return super.getItemViewType(position);
    }

    public Bitmap getBitmap(String filePath,int maxWidth,int maxHeight){
        if (!"".endsWith(filePath)){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath,options);
            options.inSampleSize = calculateInSampleSize(options,maxWidth,maxHeight);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(filePath,options);
        }
        return null;
    }

    private int calculateInSampleSize(BitmapFactory.Options options, int requireW, int requereH) {
        int sampleSize = 1;
        int outHeight = options.outHeight;
        int outWidth = options.outWidth;
        int rationHeight = Math.round(outHeight / requereH);
        int rationWidth = Math.round(outWidth / requireW);
        if (rationHeight > 1 || rationWidth > 1) {
            sampleSize = rationHeight > rationWidth ? rationWidth : rationHeight;
        }
        Log.d("tag", "outHeight = " + outHeight + "outWidth = " + outWidth + " -------- " + sampleSize);
        return sampleSize;
    }

}
